/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ViewCateRelationDto;
import com.scpii.universal.hibernate.dao.AppViewCatelogRalationDao;
import com.scpii.universal.hibernate.entity.AppViewCatelogRalationTab;


/**
 * universalWeb
 * @author liuhj
 * 2012-7-23下午05:24:44
 */
@Repository
public class AppViewCatelogRalationDaoImpl extends BaseDaoImpl<AppViewCatelogRalationTab, Integer> implements AppViewCatelogRalationDao{

	@Override
	public void delete(String cateIds) {
		String hql = "update AppViewCatelogRalationTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intCateId in" + cateIds;
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(new Date())).executeUpdate();
	}

	@Override
	public AppViewCatelogRalationTab getRelationById(int id, String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intViewCateId=?";
		return (AppViewCatelogRalationTab) getHibernateTemplate()
				.getSessionFactory().getCurrentSession().createQuery(hql)
				.setParameter(0, id).uniqueResult();
	}

	@Override
	public void deleteByViewAndCateIds(int viewId, String cateIds) {
		String hql = "update AppViewCatelogRalationTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intCateId in" + cateIds + " and a.intViewId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
					.setParameter(2, viewId).executeUpdate();
	}

	@Override
	public void delete(Integer id) {
		String hql = "update AppViewCatelogRalationTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intViewCateId=? ";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
					.setParameter(2, id).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean checkIfExistRelation(int viewId, int cateId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new AppViewCatelogRalationTab(a.intViewCateId) from AppViewCatelogRalationTab a ");
		hql.append("where a.intDeleteSign=? and a.intCateId=? and a.intViewId=?");
		List<AppViewCatelogRalationTab> count = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql.toString()).setParameter(
						0, GenericConstants.DELETE_SIGN_EXIST).setParameter(1,
						cateId).setParameter(2, viewId).list();
		if (count != null && count.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteRelationsByViewId(int intViewId) {
		String hql = "update AppViewCatelogRalationTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
			+ "where a.intViewId=? ";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
			.createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_DELETED).setParameter(
					1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
					.setParameter(2, intViewId).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppViewCatelogRalationTab> getRelationsByViewId(int viewId,
			String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intViewId=?";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(
						0,GenericConstants.DELETE_SIGN_EXIST).
						setParameter(1, viewId).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<ViewCateRelationDto> getRelationsByViewId(int viewId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new com.scpii.universal.dto.ViewCateRelationDto(");
		hql.append("a.intViewCateId,a.intCateId,a.intResourceId,a.intContentType");
		hql.append(",c.varCateName,c.varCateImgUrl,c.varCateDescrip)");
		hql.append(" from AppViewCatelogRalationTab a,CatelogTab c");
		hql.append(" where a.intCateId=c.intCateId");
		hql.append(" and a.intDeleteSign=? and a.intViewId=?");
		hql.append(" order by a.intSortOrder desc,a.intViewCateId asc");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString()).setParameter(
						0,GenericConstants.DELETE_SIGN_EXIST).
						setParameter(1, viewId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ViewCateRelationDto> getRelationsByCateId(int cateId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new com.scpii.universal.dto.ViewCateRelationDto(");
		hql.append("a.intViewCateId,a.intResourceId,a.intViewId)");
		hql.append(" from AppViewCatelogRalationTab a");
		hql.append(" where a.intDeleteSign=?");
		hql.append(" and a.intCateId=?");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString()).setParameter(
						0,GenericConstants.DELETE_SIGN_EXIST).
						setParameter(1, cateId).list();
	}

	@Override
	public AppViewCatelogRalationTab getRelationByPageId(int pageId,
			String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intResourceId=?";
		return (AppViewCatelogRalationTab) getHibernateTemplate()
				.getSessionFactory().getCurrentSession().createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, pageId).uniqueResult();
	}
	
}
